package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.domain.ResultEntity;
import com.bjpowernode.crm.settings.domain.DicType;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.service.DicTypeService;
import com.bjpowernode.crm.settings.service.DicValueService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 郭俊才
 * 2021/8/16
 */
@Controller
public class DicValueController {
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private DicTypeService dicTypeService;
    private Logger logger = LoggerFactory.getLogger(DicValueController.class);

    //点击数据字典中的创建按钮跳到数据字典添加页面页
    @RequestMapping("/settings/dictionary/value/index.do")
    public String getDicValues(ModelMap modelMap){
        List<DicValue> dicValueList = dicValueService.queryAll();
        modelMap.addAttribute("dicTypeList",dicValueList);
        return "settings/dictionary/value/index";
    }

    //添加
    @RequestMapping("settings/dictionary/value/save.do")
    @ResponseBody
    public ResultEntity createDicType(DicValue dicValue){
        int saveNum = dicValueService.save(dicValue);
        if(saveNum == 1){
            return  ResultEntity.successWithoutData();
        }else {
            return  ResultEntity.failed("添加失败");
        }
    }
    //修改页面回显数据
    @RequestMapping("settings/dictionary/value/getDicType.do")
    public String getDicType(String id,ModelMap modelMap){
        DicValue dicValue = dicValueService.queryByPrimaryKey(id);
        modelMap.addAttribute("dicValue",dicValue);
        return "settings/dictionary/value/edit";
    }
    //修改
    @RequestMapping("settings/dictionary/value/edit.do")
    @ResponseBody
    public ResultEntity changeDicType(DicValue dicValue){
        int i = dicValueService.editByPrimaryKey(dicValue);
        if(i == 1){
            return  ResultEntity.successWithoutData();
        }else {
            return  ResultEntity.failed("修改失败");
        }
    }
    //删除
    @RequestMapping("settings/dictionary/value/remove.do")
    @ResponseBody
    public ResultEntity dropDicType(@RequestBody String[] ids){
        int i = dicValueService.removeByPrimaryKey(ids);
        if(i > 0){
            return  ResultEntity.successWithoutData();
        }else {
            return  ResultEntity.failed("删除失败");
        }
    }
    //添加页面回显字典类型数据
    @RequestMapping("/settings/dictionary/value/tosave.do")
    public String toDictionaryValueSave(ModelMap modelMap){
        List<DicType> dicTypes = dicTypeService.queryAll();
        modelMap.addAttribute("dicTypes",dicTypes);
        return "settings/dictionary/value/save";
    }
}